package com._520it.wms.web.controller;

import com._520it.wms.domain.Employee;
import com._520it.wms.query.EmployeeQueryObject;
import com._520it.wms.service.IDepartmentService;
import com._520it.wms.service.IEmployeeService;
import com._520it.wms.service.IRoleService;
import com._520it.wms.util.JSONResult;
import com._520it.wms.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by HASEE on 2017/11/14.
 */
@Controller
@RequestMapping("/employee")
public class EmployeeContorller {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

    @RequestMapping("/list")
    @RequiredPermission("员工列表")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo) {
        model.addAttribute("pageResult", employeeService.query(qo));
        //查询所有部门
        model.addAttribute("departments", departmentService.listAll());
        return "/employee/list";
    }

    @RequestMapping("/input")
    @RequiredPermission("员工编辑")
    public String input(Long id, Model model) {
        //查询所有的部门
        model.addAttribute("departments", departmentService.listAll());
        model.addAttribute("roles", roleService.listAll());
        if (id != null) {
            Employee employee = employeeService.get(id);
            model.addAttribute("employee", employee);
        }
        return "/employee/input";
    }

    @RequestMapping("delete")
    @RequiredPermission("员工删除")
    @ResponseBody
    public JSONResult delete(Long id) {
        if (id != null) {
            employeeService.delete(id);
        }
        return new JSONResult();
    }

    @RequestMapping("/saveOrUpdate")
    @RequiredPermission("员工新增/更新")
    @ResponseBody
    public JSONResult saveOrUpdate(Employee employee, Long[] roleIds) {
        employeeService.saveOrUpdate(employee, roleIds);
        return new JSONResult();

    }

    @RequestMapping("/batchDelete")
    @ResponseBody
    @RequiredPermission("员工批量删除")
    public JSONResult batchDelete(Long[] ids) {
        employeeService.batchDelete(ids);
        return new JSONResult();

    }

}
